package com.yyc.produce.controller;


import com.yyc.produce.VO.ProductInfoVO;
import com.yyc.produce.VO.ProductVO;
import com.yyc.produce.VO.ResultVO;
import com.yyc.produce.dataobject.ProductCategory;
import com.yyc.produce.dataobject.ProductInfo;
import com.yyc.produce.service.CategoryService;
import com.yyc.produce.service.ProductService;
import com.yyc.produce.utils.ResultVOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品
 * @author yyc
 */
@RestController
@RequestMapping("/product")
public class ProduceController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService ;


    /**
     * 1.查询所有在架商品
     * 2.获取类目type
     * 3.查询类目
     * 4.构造函数
     */
    @GetMapping("/list")
    public ResultVO List(){
        List<ProductInfo> productInfoList = productService.findUpAll();
        List<Integer> categoryTypeList = productInfoList.stream()
                .map(ProductInfo::getCategoryType)
                .collect(Collectors.toList());

        List<ProductCategory> categoryList = categoryService.findByCategoryTypeIn(categoryTypeList);

        List<ProductVO> productVOList = new ArrayList<>();
        for (ProductCategory productCategory :categoryList){
            ProductVO productVO = new ProductVO();
            productVO.setCategoryName(productCategory.getCategoryName());
            productVO.setCategoryType(productCategory.getCategoryType());
            List<ProductInfoVO> productInfoVOList= new ArrayList<>();
            for (ProductInfo productInfo:productInfoList) {
                if (productInfo.getCategoryType().equals(productCategory.getCategoryType())){
                ProductInfoVO productInfoVO = new ProductInfoVO();
                BeanUtils.copyProperties(productInfo,productInfoVO);
                }
            }
            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);

        }
        return ResultVOUtil.success(productVOList);

    }

}
